package com.kongwg.java;

import org.junit.Test;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author kongwg
 * @create 2020-12-18 11:32
 */
public class FileReadWriterTest {


    public static void main(String[] args) {
        File file = new File("hello.txt");
        System.out.println(file.getAbsolutePath());

        File file1 = new File("day09\\hello.txt");
        System.out.println(file1.getAbsolutePath());
    }


    @Test
    public void test1() {
        FileReader fr = null;
        try {
            File file = new File("hello.txt");
            //提供流
            fr = new FileReader(file);

//        //数据读入
//        int data = fr.read();
//        while (data != -1) {
//            System.out.print((char)data);
//            data = fr.read();
//        }

            //方式二
            int data;
            while ((data = fr.read()) != -1) {
                System.out.println((char) data);

            }
        } catch (IOException e) {
            e.getMessage();
        } finally {
            //
            try {
                if (fr != null)
                    fr.close();
            } catch (IOException e) {
                e.getMessage();
            }
        }
    }

    @Test
    public void testFileReader1() {
        FileReader fr = null;
        try {
            //定义file
            File file = new File("hello.txt");
            //实例化流
            fr = new FileReader(file);
            //操作数据
            char[] cbuf = new char[5];
            int len;
            while ((len = fr.read(cbuf)) != -1) {
//                for (int i = 0; i < len; i++) {
//                    System.out.print(cbuf[i]);
//                }
                //方式二
                System.out.println(len);
                String s = new String(cbuf, 0, len);
                System.out.print(s);
            }

        } catch (IOException e) {
            e.getMessage();
        } finally {
            if (fr != null) {
                try {
                    //关闭流
                    fr.close();
                } catch (IOException e) {
                    e.getMessage();
                }

            }
        }

    }

    @Test
    public void FileWriterUtilTest() throws IOException {

        //file实例化
        File file = new File("hello1.txt");
        //输出流outputStream实例化
        FileWriter fw = new FileWriter(file, true);
        //操作数据
        fw.write("i have a dream!\n");
        fw.write("you need have a dream!\n");
        //关闭数据
        fw.close();
    }

    @Test
    public void ReaderWriterUtilTest() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            //实例化读入和写出file
//            File srcFile = new File("hello1.txt");
//            File descFile = new File("hello2.txt");

            File srcFile = new File("爱情与友情.jpg");
            File descFile = new File("爱情与友情1.jpg");
            //实例化Reader和Writer
            fr = new FileReader(srcFile);
            fw = new FileWriter(descFile);
            //操作数据
            char[] cbuf = new char[5];

            int len;//每次读取到数组中的长度
            while ((len = fr.read(cbuf)) != -1) {
                fw.write(cbuf, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭数据流
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    if (fw != null)
                        fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
